package com.situ.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Order;
import com.situ.crm.mapper.OrderMapper;
import com.situ.crm.service.IOrderService;
import com.situ.crm.vo.OrderCountVO;

@Service
public class OrderServiceImpl implements IOrderService {
	@Autowired
	private OrderMapper orderMapper;
	
	@Override
	public ServerResponse pageList(Integer page, Integer limit, Order order) {
		PageHelper.startPage(page,limit);
		List<Order> list = orderMapper.pageList(order);
		
		PageInfo pageInfo = new PageInfo<>(list);
		Integer count = (int)pageInfo.getTotal();
		return ServerResponse.createSuccess("查找成功", count, list);
	}
	
	//添加
	@Override
	public ServerResponse insert(Order order) {
		try {
			Integer count = orderMapper.insert(order);
			if (count == 1) {
				return ServerResponse.createSuccess("添加成功");
			} else {
				return ServerResponse.createError("添加失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("添加失败");
		}
	}

	//根据id删除
	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			Integer count = orderMapper.deleteByPrimaryKey(id);
			if (count == 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	//批量删除
	@Override
	public ServerResponse deleteAll(String ids) {
		String[] idArrays = ids.split(",");
		try {
			Integer count = orderMapper.deleteAll(idArrays);
			if (count >= 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createError("删除失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("删除失败");
		}
	}

	//修改查询
	@Override
	public Order select(Integer id) {
		return orderMapper.selectByPrimaryKey(id);
	}

	//修改
	@Override
	public ServerResponse update(Order order) {
		try {
			Integer count = orderMapper.updateByPrimaryKeySelective(order);
			if (count == 1) {
				return ServerResponse.createSuccess("修改成功");
			} else {
				return ServerResponse.createError("修改失败");
			}
		} catch (Exception e) {
			return ServerResponse.createError("修改失败");
		}
	}

	//ECharst统计销量
	@Override
	public ServerResponse selectOrderCount() {
		List<OrderCountVO> list = orderMapper.selectOrderCount();
		return ServerResponse.createSuccess("统计成功", list);
	}

}
